package com.yoyo.yyadsdk.serv;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.yoyo.yyadsdk.IService_;
import com.yoyo.yyadsdk.main.KernelService;

public class SystemService extends Service {
    private MyBinder binder;
    private ServiceConnection conn;

    @Override
    public void onCreate() {
        super.onCreate();
        init();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent intents = new Intent();
        intents.setClass(this, KernelService.class);
        bindService(intents, conn, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    private void init() {
        if (conn == null) {
            conn = new MyConnection();
        }
        binder = new MyBinder();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    static class MyBinder extends IService_.Stub {

        @Override
        public String getName() {
            return "Remote";
        }
    }

    class MyConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
//            System.out.println("获取远程连接");
        }

        @Override
        public void onServiceDisconnected(ComponentName nme) {
//            System.out.println("本地连接被干掉了");
            startService(new Intent(SystemService.this, KernelService.class));
            bindService(new Intent(SystemService.this, KernelService.class), conn, Context.BIND_IMPORTANT);
        }
    }

}